package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.FansFollow;
import cn.wolfcode.wolf2w.repository.FansFollowRepository;
import cn.wolfcode.wolf2w.service.IFansFollowService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class FansFollowServiceImpl implements IFansFollowService {
    @Autowired
    private FansFollowRepository repository;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IUserInfoService userInfoService;

    @Override
    public void save(FansFollow fansFollow) {
        fansFollow.setId(null);
        repository.save(fansFollow);
    }

    @Override
    public void update(FansFollow fansFollow) {
        repository.save(fansFollow);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public FansFollow get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<FansFollow> list() {
        return repository.findAll();
    }

    @Override
    public void follow(String followId, String id) {
        AssertUtil.hasLength(followId, "参数错误");
        // 当前用户的粉丝关注信息
        FansFollow ffs = repository.findByUid(id);
        // 被关注人的粉丝关注信息
        FansFollow followed = repository.findByUid(followId);
        AssertUtil.notEquals(followId, id, "不能关注自己");
        if (ffs == null) {
            FansFollow fansFollow = new FansFollow();
            fansFollow.setUid(id);
            fansFollow.setFansId(new ArrayList<>());
            List<String> list = new ArrayList<>();
            list.add(followId);
           fansFollow.setFollowId(list);
           this.save(fansFollow);
            // 插入粉丝数据
            insertFans(followed, followId, id);
        }else {
            List<String> list = ffs.getFollowId();
            if (list.contains(followId)) {
                list.remove(followId);
            }
            list.add(0, followId);
            ffs.setFollowId(list);
            this.update(ffs);
            // 插入粉丝数据
            insertFans(followed, followId, id);
        }
    }

    /**
     * 插入对应的粉丝数据
     * @param followed 被关注的对象
     * @param followId 被关注的对象id
     * @param id 当前用户的id
     */
    private void insertFans(FansFollow followed, String followId, String id){
        // 插入粉丝id
        if (followed == null){
            FansFollow fe = new FansFollow();
            fe.setUid(followId);
            List<String> list1 = new ArrayList<>();
            list1.add(id);
            fe.setFansId(list1);
            fe.setFollowId(new ArrayList<>());
            this.save(fe);
        } else{
            List<String> fansId = followed.getFansId();
            if (fansId.contains(id)) {
                fansId.remove(id);
            }
            fansId.add(0, id);
            followed.setFansId(fansId);
            this.update(followed);
        }
    }

    @Override
    public FansFollow findByUid(String uid) {
        return repository.findByUid(uid);
    }
}
